//
//  DYReleaseHeadlinesCell.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/26.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYReleaseHeadlinesCell.h"



@interface DYReleaseHeadlinesCell ()<UITextViewDelegate,UINavigationControllerDelegate,TZImagePickerControllerDelegate>

@property (nonatomic,strong)TZImagePickerController *imagePicker;
@property (nonatomic, strong) NSMutableAttributedString *attributedString;
@property (nonatomic, assign) BOOL isClear;

/** H1*/
@property (nonatomic, assign) BOOL isH1;

@end

@implementation DYReleaseHeadlinesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(void)dy_initUI
{
    [super dy_initUI];
    
    self.sideMargin=BILIWIDTH(10);
    [self xhq_noneSelectionStyle];
    
    self.titleLabel=({
        UILabel *label=[Utils labelWithTitleFontSize:12 textColor:[UIColor xhq_content] alignment:0];
        label.text=@"店铺简介";
        label;
    });
    
    self.infoTextView = ({
        UITextView *textView=[[UITextView alloc]init];
        textView.font=kFont(14);
        textView.textColor=[UIColor xhq_content];
        textView.backgroundColor=XHQHexColor(0xFAFAFA);
        textView.textContainerInset = UIEdgeInsetsMake(BILIWIDTH(10),BILIWIDTH(5),BILIWIDTH(10), BILIWIDTH(5));
        textView.dy_placeHolder=@"请输入店铺简介...";
        [textView xhq_borderColor:[UIColor xhq_line] borderWidth:BILIWIDTH(0.7)];
        textView.inputAccessoryView = [self createInputAccessoryView];
        textView.delegate=self;
        textView;
    });
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.infoTextView];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(10));
        make.top.equalTo(BILIHEIGHT(10));
    }];
    
    [self.infoTextView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(BILIWIDTH(10));
        make.right.equalTo(BILIWIDTH(-10));
        make.top.equalTo(self.titleLabel.bottom).offset(BILIHEIGHT(7));
        make.bottom.equalTo(BILIHEIGHT(-10));
    }];
    
    
    @weakify(self);
    [self.imagePicker setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        @strongify(self);
        NSTextAttachment *attachment=[[NSTextAttachment alloc] initWithData:nil ofType:nil];
        attachment.image=photos.lastObject;
//        attachment.bounds=CGRectMake(0, -10, 200, 200);
        attachment.bounds=CGRectMake(0, -10,BILIWIDTH(330),BILIWIDTH(330)*kScreenHeight/kScreenWidth);
        NSAttributedString *attributedString=[NSAttributedString attributedStringWithAttachment:attachment];
        [self.attributedString appendAttributedString:attributedString];
        self.infoTextView.attributedText = self.attributedString;
        [self.infoTextView becomeFirstResponder];
    }];
    
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"fuwenben" ofType:@"txt"];
    ;
    NSString *content = [[NSString alloc] initWithData:[[NSData alloc] initWithContentsOfFile:path] encoding:NSUTF8StringEncoding];
    NSData *data = [content dataUsingEncoding:NSUnicodeStringEncoding];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.infoTextView.attributedText = attributedString;
    self.attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
}
#pragma mark - UITextViewDelegate
-(void)textViewDidChange:(UITextView *)textView
{
    if (self.isClear) {
        self.attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
        return;
    }
    textView.attributedText = self.attributedString;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (text.length < 1) {
        self.isClear = YES;
        return YES;
    }
    self.isClear = NO;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if (self.isH1) {
         [dict setValue:kFont(20) forKey:NSFontAttributeName];
    }else{
        [dict setValue:kFont(14) forKey:NSFontAttributeName];
    }
    
    NSAttributedString *attr = [[NSAttributedString alloc]initWithString:text
                                                              attributes:dict];
    [self.attributedString appendAttributedString:attr];
    
    return YES;
}
- (void)fontAction:(UIBarButtonItem *)item {
    if (self.isH1) {
        item.tintColor = nil;
    }else { // 选中
        item.tintColor = [UIColor redColor];
    }
    self.isH1 = !self.isH1;
}
- (UIView *)createInputAccessoryView {
    // 键盘工具栏
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_pic"] style:UIBarButtonItemStylePlain target:self action:@selector(imageAction)];
    UIBarButtonItem *th1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_words"] style:UIBarButtonItemStylePlain target:self action:@selector(fontAction)];
    UIBarButtonItem *th2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style: UIBarButtonItemStyleDone target: self action: @selector(done)];
    item3.tintColor=[UIColor xhq_aTitle];
    UIBarButtonItem *th3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

    
    UIToolbar *tool = [UIToolbar new];
    [tool sizeToFit];
//    [tool setItems:@[item1,th1, item2, th2, item3, th3, item4, th4, item5, th5, item6]];
     [tool setItems:@[item1,th1, item2, th2,th3,item3]];
    return tool;
}
-(void)imageAction
{
    [[DYAppCommon getCurrentVC] presentViewController:self.imagePicker animated:YES completion:nil];
}
-(void)fontAction
{
     self.isH1 = !self.isH1;
}
-(void)done
{
    [self.infoTextView resignFirstResponder];
    
    
//    NSData *data = [self.attributedString dataFromRange:NSMakeRange(0, self.attributedString.length) documentAttributes:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} error:nil];
//    NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"html = %@",html);
//    
//    NSRange srcStartRange = [html rangeOfString:@"<img src=\"file:///"];
//    if (srcStartRange.location == NSNotFound || srcStartRange.length == 0) {
//        return;
//    }
//    //                NSUInteger location = srcStartRange.location + srcStartRange.length;
//    //                NSUInteger length = htmlStr.length - location;
//    //                NSRange srcEndRange = [htmlStr rangeOfString:@"alt" options:NSAnchoredSearch range:NSMakeRange(srcStartRange.location + srcStartRange.length, length)];
//    NSRange srcEndRange = [html rangeOfString:@".png\">"];
//    if (srcEndRange.location == NSNotFound || srcEndRange.length == 0) {
//        return;
//    }
//    NSLog(@"%lu  %lu.....",(unsigned long)srcStartRange.location,(unsigned long)srcEndRange.location);
//    // model.path
//    NSString *temp = [NSString stringWithFormat:@"http://120.25.95.75:8080/imgfile/uploadFiles/uploadImgs/html/img/20170622/1498123794135065031.jpeg\"/"];
//    NSUInteger location = srcStartRange.location+10;
//    NSUInteger length = srcEndRange.location - srcStartRange.location - 10 + 5;
//    NSString *htmlStr = [html stringByReplacingCharactersInRange:NSMakeRange(location, length) withString:temp];
//    NSLog(@"htmlStr = %@",htmlStr);
}


-(TZImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker=[[TZImagePickerController alloc]initWithMaxImagesCount:9 delegate:self];
        _imagePicker.preferredLanguage=@"zh-Hans";
        _imagePicker.allowPickingVideo=NO;
        _imagePicker.naviBgColor=[UIColor whiteColor];
        _imagePicker.naviTitleColor=[UIColor xhq_aTitle];
        _imagePicker.barItemTextColor=[UIColor xhq_aTitle];
        _imagePicker.showSelectBtn=YES;
    }
    return _imagePicker;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
